package com.dpj.servlet;

import com.dpj.pojo.User;
import com.dpj.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private UserService userService=new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkCode = req.getParameter("checkCode");

        HttpSession session = req.getSession();
        String code = (String) session.getAttribute("code");
        if(code.equalsIgnoreCase(checkCode)){
            userService.insertInto(username,password);
            session.setAttribute("msg","注册成功，请登录");
            resp.sendRedirect("/w9/login.jsp");
        }else {
            req.setAttribute("msg","验证码有误，请重新输入");
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }
    }
}
